@extends('admin.base')

@section('content')
    <div class="layui-card">
        <div class="layui-card-header layuiadmin-card-header-auto">
            <div class="layui-btn-group">
                <button class="layui-btn layui-btn-sm" id="create">添 加</button>
                <button class="layui-btn layui-btn-sm" id="search">搜 索</button>
            </div>
            <form class="layui-form">
                <div class="layui-form-item">
                    <div class="layui-input-inline">
                        <input type="text" name="search" id="title" placeholder="标题搜索" class="layui-input" >
                    </div>
                    <div class="layui-input-inline">
                        <select name="type"  id="type" lay-filter="type">
                            <option value="">请选择栏目</option>
                            @foreach($tree as $key=>$val)
                                @if(isset($dao) && $dao->type_id == $val['id'])
                                    <option value="{{$val['id']}}" selected >{!! str_repeat('&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;', intval($val['lv'])) !!} {{$val['name']}}</option>
                                @else
                                    <option value="{{$val['id']}}" >{!! str_repeat('&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;', intval($val['lv'])) !!}{{$val['name']}}</option>
                                @endif
                            @endforeach
                        </select>
                    </div>
                </div>
            </form>
        </div>
        <div class="layui-card-body">
            <table id="dataTable" lay-filter="dataTable"></table>
            <script type="text/html" id="coverTpl">
                <a href ="javascript:void(0)"><img src="@{{d.cover}}" alt="" style="width: 100%;height: 100%;"></a>
            </script>
            <script type="text/html" id="options">
                <div class="layui-btn-group">
                    <a class="layui-btn layui-btn-sm" lay-event="edit">编辑</a>
                    <a class="layui-btn layui-btn-sm layui-btn-danger" lay-event="del">删除</a>
                </div>
            </script>
        </div>

    </div>
@endsection

@section('script')
    <script>
        layui.use(['layer','table','form','laytpl'],function () {
            var $ = layui.jquery;
            var layer = layui.layer;
            var form = layui.form;
            var table = layui.table;
            var laytpl = layui.laytpl;

            //用户表格初始化
            var dataTable = table.render({
                elem: '#dataTable'
                ,height: 500
                ,url: "/admin/api/articles" //数据接口
                ,page: true //开启分页
                ,id:'d'
                ,cols: [[ //表头
                    {checkbox: true,fixed: true}
                    ,{field: 'id', title: 'ID', sort: true,width:80}
                    ,{field: 'title', title: '标题'}
                    ,{field: 'cover', width: 120, title: '封面图片', templet:'#coverTpl',event:'cover'}
                    ,{field: 'typeName', title: '所属栏目'}
                    ,{field: 'updated_at', title: '更新时间'}
                    ,{fixed: 'right', width: 150,title:'操作', align:'center', toolbar: '#options'}
                ]]
            });

            //搜索
            $('#search').click(function () {
                var title = $('#title').val();
                var type = $('#type').val();
                dataTable.reload({
                    where:{
                        title:title,
                        type:type
                    }
                    ,page: {
                        curr: 1 //重新从第 1 页开始
                    }
                });
            });
            //下拉框直接搜索
            form.on('select(type)', function(data){
                var type = $('#type').val();
                dataTable.reload({
                    where:{
                        type:type
                    }
                    ,page: {
                        curr: 1 //重新从第 1 页开始
                    }
                });
            });

            //监听工具条
            table.on('tool(dataTable)', function(obj){ //注：tool是工具条事件名，dataTable是table原始容器的属性 lay-filter="对应的值"
                var data = obj.data //获得当前行数据
                    ,layEvent = obj.event; //获得 lay-event 对应的值
                if(layEvent === 'edit'){
                    location.href = '/admin/view/article/'+data.id+'/edit';
                } else if(layEvent === 'cover') {
                    if (data.cover !== '') {
                        let html = '<img src="'+data.cover+'" alt="" style="width: 100%;height: 100%;">';
                        layer.open({
                            title:'',
                            type:1,
                            ShadeClose:true,
                            content:html
                        });
                    }
                } else if(layEvent === 'del'){
                    state(data.id,0);
                }
            });

            function state(id,state) {
                layer.confirm('请确认是否删除',function () {
                    $.ajax({
                        type:'POST',
                        url:"/admin/api/article/"+id+"/state/"+state,
                        data:{_method:"PUT"},
                        dataType:'json',
                        success:function (response) {
                            if (response.code === 0 ){
                                layer.msg(response.message,{icon:1});
                                dataTable.reload();
                            }else{
                                layer.msg(response.message,{icon:6});
                            }
                        }
                    });
                    return false;
                });
            }

            $('#create').click(function () {
                location.href = '/admin/view/article/store';
            });

            //按钮批量禁用
            $("#listDelete").click(function () {
                var ids = [];
                var hasCheck = table.checkStatus('d');
                var hasCheckData = hasCheck.data;

                if (hasCheckData.length>0){
                    $.each(hasCheckData,function (index,element) {
                        ids.push(element.id)
                    });
                }

                if (ids.length>0){
                    layer.confirm('确认禁用？', function(index){
                        $.get("/admin/article/state",{ids:ids,state:0},function (result) {
                            if (result.code === 0){
                                dataTable.reload();
                            }
                            layer.close(index);
                            layer.msg(result.message,{icon:6})
                        });
                    });
                }else {
                    layer.msg('请选择操作项',{icon:5})
                }
            })
        })
    </script>
@endsection



